import javax.swing.JPanel;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JSeparator;

import java.awt.Color;
import java.awt.Component;

import javax.swing.Box;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JButton;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseListener;

import javax.swing.UIManager;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


 class PlayerInterface extends JPanel  {

	 Card hand[] = new Card[5];
	 JLabel C1,C2,C3,C4,C5;
	 JButton Exchange, Ready;
	 Bot Controller=null;
	 int c = 0,d = 0,f = 0,g = 0,h = 0,Ex_permission = 0,Re_permission = 0; //Seems that you don't know the alphabet, do you?
	 Deck deck;
	 Prologue superClass;
	 CheckSystem Game;
	/**
	 * Create the panel.
	 */
	PlayerInterface(int x,int y,Prologue getSuper) {
		this.superClass=getSuper;
		this.deck = this.superClass.deck;
		Game=new CheckSystem();
		setBackground(UIManager.getColor("Button.background"));
		setLayout(null);
		if(y==0) {
			JLabel PlayerName = new JLabel("Player "+x);
			PlayerName.setFont(new Font("Tahoma", Font.PLAIN, 17));
			PlayerName.setBounds(106, 11, 99, 30);
			add(PlayerName);
		}
		else {
			this.Controller=new Bot(this);
			JLabel PlayerName = new JLabel("Bot "+x);
			PlayerName.setFont(new Font("Tahoma", Font.PLAIN, 17));
			PlayerName.setBounds(106, 11, 99, 30);
			add(PlayerName);
		}
			
		
		
		JLabel Cards = new JLabel("Cards:");
		Cards.setFont(new Font("Tahoma", Font.PLAIN, 13));
		Cards.setBounds(10, 54, 46, 14);
		add(Cards);
		
		C1 = new JLabel("C1");
		C1.setForeground(UIManager.getColor("Button.foreground"));
		C1.setBackground(UIManager.getColor("Button.background"));
		C1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if(c == 0) {
					C1.setForeground(Color.red);
					c = 1;
				}
				else {
					C1.setForeground(Color.black);
					c = 0;
				}
			}
		});
		C1.setToolTipText("");
		C1.setLabelFor(this);
		C1.setFont(new Font("Tahoma", Font.PLAIN, 13));
		C1.setBounds(66, 54, 22, 14);
		add(C1);
		
		C2 = new JLabel("C2");
		C2.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if(d == 0) {
					C2.setForeground(Color.red);
					d = 1;
				}
				else {
					C2.setForeground(Color.black);
					d = 0;
				}
			}
		});
		C2.setFont(new Font("Tahoma", Font.PLAIN, 13));
		C2.setBounds(103, 54, 22, 14);
		add(C2);
		
		C3 = new JLabel("C3");
		C3.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if(f == 0) {
					C3.setForeground(Color.red);
					f = 1;
				}
				else {
					C3.setForeground(Color.black);
					f = 0;
				}
			}
		});
		C3.setFont(new Font("Tahoma", Font.PLAIN, 13));
		C3.setBounds(140, 54, 22, 14);
		add(C3);
		
		C4 = new JLabel("C4");
		C4.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if(g == 0) {
					C4.setForeground(Color.red);
					g = 1;
				}
				else {
					C4.setForeground(Color.black);
					g = 0;
				}
			}
		});
		C4.setFont(new Font("Tahoma", Font.PLAIN, 13));
		C4.setBounds(177, 54, 22, 14);
		add(C4);
		
		C5 = new JLabel("C5");
		C5.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if(h == 0) {
					C5.setForeground(Color.red);
					h = 1;
				}
				else {
					C5.setForeground(Color.black);
					h = 0;
				}
			}
		});
		C5.setFont(new Font("Tahoma", Font.PLAIN, 13));
		C5.setBounds(214, 54, 22, 14);
		add(C5);
		
		Exchange = new JButton("Exchange");
		Exchange.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				exchangeCards();
			}
				
		});
		Exchange.setBounds(10, 119, 108, 23);
		add(Exchange);
		
		Ready = new JButton("Ready");
		Ready.addActionListener(new ActionListener() {	
			@Override
			public void actionPerformed(ActionEvent e) {
				readyCheck();
			}
		});
		Ready.setBounds(10, 153, 108, 23);
		add(Ready);

	}
	
	void exchangeCards()
	{
		{
			if(Ex_permission == 0) {
				if(c == 1 & d == 1 & f == 1 & g == 1 & h == 1) {
					C1.setForeground(Color.black);
					c = 0;
					C2.setForeground(Color.black);
					d = 0;
					C3.setForeground(Color.black);
					f = 0;
					C4.setForeground(Color.black);
					g = 0;
					C5.setForeground(Color.black);
					h = 0;
				}
				else {
					if(c == 1) {
						hand[0] = deck.getCard();
						C1.setForeground(Color.black);
						c = 0;
					}
					if(d == 1) {
						hand[1] = deck.getCard();
						C2.setForeground(Color.black);
						d = 0;
					}
					if(f == 1) {
						hand[2] = deck.getCard();
						C3.setForeground(Color.black);
						f = 0;
					}
					if(g == 1) {
						hand[3] = deck.getCard();
						C4.setForeground(Color.black);
						g = 0;
					}
					if(h == 1) {
						hand[4] = deck.getCard();
						C5.setForeground(Color.black);
						h = 0;
					}
					sortHand();
					loadHand();
					Ex_permission = 1;

				}
			}
		}
	}
	
	void readyCheck()
	{
		Ex_permission = 1;
		if(this.Re_permission == 0)
		{
			this.Re_permission=1;
			this.superClass.readyCheck--;
			if(4-superClass.readyCheck==superClass.PlayerNum)
			{
				for (int i=1;i<4;i++)
				{
					if(superClass.Players[i].Controller!=null)
					{
						superClass.Players[i].Controller.startTurn();
					}
					//go find a winner
					
				}
				String q = "";
				q = this.Game.WinnerCheck(superClass.Players[0],superClass.Players[1],superClass.Players[2],superClass.Players[3]);
				String x="";
				
				switch (q)
				{
				case "1":
					x="The Winner is Player 1!";
					break;
				case "2":
					x="The Winner is Player 2!";
					break;
				case "3":
					x="The Winner is Player 3!";
					break;
				case "4":
					x="The Winner is Player 4!";
					break;
				case "12":
					x="Tie between Player 1 and Player 2!";
					break;
				case "13":
					x="Tie between Player 1 and Player 3!";
					break;
				case "14":
					x="Tie between Player 1 and Player 4!";
					break;
				case "23":
					x="Tie between Player 2 and Player 3!";
					break;
				case "24":
					x="Tie between Player 2 and Player 4!";
					break;
				case "34":
					x="Tie between Player 3 and Player 4!";
					break;
				case "123":
					x="Tie between Player 1, Player 2 and Player 3!";
					break;
				case "134":
					x="Tie between Player 1, Player 3 and Player 4!";
					break;
				case "124":
					x="Tie between Player 1, Player 2 and Player 4!";
					break;
				case "234":
					x="Tie between Player 2, Player 3 and Player 4!";
					break;
				case "1234":
					x="!!!QUADRA TIE!!!";
					break;
				}
				
				JOptionPane.showMessageDialog(this,
		        x,
		        "Poker - Info",
		        JOptionPane.INFORMATION_MESSAGE);
		        return;	
			}
		}
		
			
	}
	
	void sortHand()
	{
		int n=5;//Hand.length()=5;
		int value1=-1,value2=-1;
		int char1=-1,char2=-1;
		Card temp;
		String figureTab[]={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
		char colourTab[]={'S','D','H','C'};
		do
		{
			for (int i=0;i<n-1;i++)
			{
				for(int j=0;j<13;j++)
				{
					if(hand[i].figure==figureTab[j])
						value1=j;
					if(hand[i+1].figure==figureTab[j])
						value2=j;						
				}
				if (value1>value2)
				{
					temp=hand[i+1];
					hand[i+1]=hand[i];
					hand[i]=temp;
				}
				else
					if(value1==value2)
					{
						for(int j=0;j<4;j++)
						{
							if(hand[i].colour==colourTab[j])
								char1=j;
							if(hand[i+1].colour==colourTab[j])
								char2=j;						
						}
						if (char1>char2)
						{
							temp=hand[i+1];
							hand[i+1]=hand[i];
							hand[i]=temp;
						}
					}
			}
			n--;
		}
		while(n>1);
	}
	
	void loadHand()
	{
		
		C1.setText(hand[0].showCard());
		C2.setText(hand[1].showCard());
		C3.setText(hand[2].showCard());
		C4.setText(hand[3].showCard());
		C5.setText(hand[4].showCard());
		
		C1.setToolTipText(hand[0].fullCardName());
		C2.setToolTipText(hand[1].fullCardName());
		C3.setToolTipText(hand[2].fullCardName());
		C4.setToolTipText(hand[3].fullCardName());
		C5.setToolTipText(hand[4].fullCardName());
		
		}
}
